package com.yanchangapp.ui.presenter;

import com.yanchangapp.base.HeadApiRetrofit;
import com.yanchangapp.commom.AppConstant;
import com.yanchangapp.model.request.ContractListRequest;
import com.yanchangapp.model.request.PsListRequest;
import com.yanchangapp.model.response.ContractListResponse;
import com.yanchangapp.model.response.PsListResponse;
import com.yanchangapp.ui.activity.purchase.POrderListActivity;
import com.yanchangapp.ui.activity.purchase.contract.ContractListActivity;
import com.yanchangapp.ui.view.ContractListFgView;
import com.zxl.baselib.ui.base.BaseActivity;
import com.zxl.baselib.ui.base.BaseObserver;
import com.zxl.baselib.ui.base.BasePresenter;
import com.zxl.baselib.ui.base.ExceptionHandle;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by apple on 2019-08-19.
 * description: 合同列表p层
 */
public class ContractListFgPresenter  extends BasePresenter<ContractListFgView> {

    public ContractListFgPresenter(BaseActivity context) {
        super(context);
    }



    public void queryPsList(String status,
                            String startTime,
                            String endTime,
                            String keywords,
                            int curPage,
                            boolean isPulling){
        if (getView() != null && curPage == 1 && !isPulling){
            getView().showCoverDialog();
            ((ContractListActivity)mContext).hideCreateBtn();
        }
        // 合同列表请求...
        ContractListRequest contractsRequest =
                new ContractListRequest(status,startTime,endTime,keywords,curPage);
        HeadApiRetrofit.getInstance().queryContractsList(contractsRequest)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<ContractListResponse>() {
                    @Override
                    public void onNext(ContractListResponse contractListResponse) {
                        if (getView() != null && curPage == 1 && !isPulling){
                            getView().hideCoverDialog();
                            ((ContractListActivity)mContext).showCreateBtn();
                        }
                        if (contractListResponse.getCode() == AppConstant.REQUEST_SUCCESS){
                            if (null != getView()){
                                getView().queryContractSuccess(contractListResponse.getData().getList());
                            }
                        }else {
                            if (null != getView()){
                                getView().queryContractFailure(contractListResponse.getMsg());
                            }
                        }
                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseException exception) {
                        // 错误回调,暂时先不处理
                        if (getView() != null){
                            getView().queryContractFailure(exception.getErrorInfo());
                        }

                        if (getView() != null && curPage == 1 && !isPulling){
                            getView().hideCoverDialog();
                            ((ContractListActivity)mContext).showCreateBtn();
                        }
                    }
                });

    }


}
